package edu.sias.user.controller;


import edu.sias.common.core.controller.BaseController;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.common.core.domain.model.LoginBody;
import edu.sias.common.exception.user.CaptchaException;
import edu.sias.framework.web.service.SysLoginService;
import edu.sias.user.component.RedisComponent;
import edu.sias.user.config.AppConfig;
import edu.sias.user.constants.Constants;
import edu.sias.user.entity.dto.CreateImageCode;
import edu.sias.user.entity.dto.EmailLoginDto;
import edu.sias.user.entity.dto.SessionWebUserDto;
import edu.sias.user.entity.enums.VerifyRegexEnum;
import edu.sias.user.entity.po.UserInfo;
import edu.sias.user.entity.vo.ResponseVO;
import edu.sias.user.exception.BusinessException;
import edu.sias.user.service.EmailCodeService;
import edu.sias.user.service.UserInfoService;
import edu.sias.user.utils.Esutils;
import edu.sias.user.utils.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.AuthProvider;

/**
 * 账号控制器
 */
@Validated
@RequestMapping("/app")
@RestController("accountController")
public class AccountController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    private static final String CONTENT_TYPE = "Content-Type";
    private static final String CONTENT_TYPE_VALUE = "application/json;charset=UTF-8";

    @Autowired
    private SysLoginService loginService;
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private EmailCodeService emailCodeService;

    @Resource
    private AppConfig appConfig;

    @Resource
    private RedisComponent redisComponent;
    /**
     * 验证码
     *
     * @param response
     * @param session
     * @param type
     * @throws IOException
     */
    @RequestMapping(value = "/checkCode")
    public void checkCode(HttpServletResponse response, HttpSession session, Integer type) throws
            IOException {
        CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
        response.setHeader("Pragma", "no-cache");//禁止浏览器缓存
        response.setHeader("Cache-Control", "no-cache");//
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String code = vCode.getCode();
        if (type == null || type == 0) {
            session.setAttribute(Constants.CHECK_CODE_KEY, code);
        } else {
            session.setAttribute(Constants.CHECK_CODE_KEY_EMAIL, code);
        }

        vCode.write(response.getOutputStream());
    }

    /**
     * @Description: 发送邮箱验证码
     * @auther: laoluo
     * @date: 20:39 2023/4/1
     * @param: [session, email, checkCode, type]
     * @return: com.easypan.com.mm.vo.entity.ResponseVO
     */
    @PostMapping("/sendEmailCode")
//    @GlobalInterceptor(checkLogin = false, checkParams = true)//验证码校验
    public AjaxResult sendEmailCode(String email) {

        emailCodeService.sendEmailCode(email);
        return success(null);
    }
 /**
 * 手机号注册，发送验证码
 */
    @PostMapping("/sendPhoneCode")
    public AjaxResult sendPhoneCode(String phone) {
       emailCodeService.sendPhoneCode(phone);
        return success(null);
    }
    /**
     * 手机号登录加注册
     */
    @PostMapping("/enrollPhonCode")
    public AjaxResult enrollPhonCode(String phone,String password,String code) {

       UserInfo userInfo = userInfoService.register1(phone,password, code);
        return success(userInfo);
    }

    /**
     *邮箱注册
     * @param session
     * @param email
     * @param nickName
     * @param password
     * @param checkCode
     * @param emailCode
     * @return
     */
    @RequestMapping("/register")
    public AjaxResult register(HttpSession session,
                             String email,
                               String nickName,
                               String password,
                               String checkCode,
                               String emailCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
                throw new BusinessException("图片验证码不正确");
            }
            userInfoService.register(email, password, emailCode);
            return success(null);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }
    /**
     * 邮箱登录
     */
    @PostMapping("/emailLogin")
    public AjaxResult emailLogin(HttpSession session,String email, String password, String checkCode) {
        if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
            throw new BusinessException("图片验证码不正确");
        }
        SessionWebUserDto sessionWebUserDto = userInfoService.login(email, password);
        session.setAttribute(Constants.SESSION_KEY, sessionWebUserDto);

        return success(null);
    }

    @Autowired
    private Esutils esutils;
    @GetMapping("/insert")
    public String insertData() throws IOException {
        esutils.add("text", "1", "这是一段测试文本");
        return "数据已插入";
    }
}

